var app = getApp()
var util = require("../../utils/util.js")
var shopModel = require("../../model/shopModel.js")
var cartUtil = require("../../utils/cartUtil.js")
var userUtil = require("../../utils/userUtil.js")
var tuiModel = require("../../model/tuiModel.js")
var wxParse = require('../../plugins/wxParse/wxParse.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    tagIndex: 0,
    tag: ['详情','规格', '评价'],
    imgs: [], //图片信息
    goods: {},
    score: 5,
    tui: null,
    arr: [1, 2, 3, 4, 5]
  },
  onLoad: function(options) {
    var cusId = options.cusId
    var id = options.id
    var type = options.type
    if (cusId) { //通过分享邀请卡进入
      this.setData({
        cusId: cusId
      })
      //保存分享访问记录
      tuiModel.saveShare(id, type, cusId)
    }
    this.init(options.id, options.type)
  },
  init(id, type) {
    var requestBody = {
      id: id,
      type: type
    }
    var that = this
    util.showLoading("加载中")
    shopModel.getGoodsInfo(requestBody, res => {
      wx.hideLoading()
      if (res.status == 1) {
        util.failToast(res.msg)
        return
      }
      wxParse.wxParse('info', 'html', res.data.html, that, 5);
      that.setData({
        goods: res.data,
        imgs: res.data.imgs
      })
    })
    tuiModel.getTui(requestBody, res => {
      if (res.status == 1) {
        return
      }
      that.setData({
        tui: res.data,
        user: app.data.user
      })
    })
    shopModel.getEvaluates(requestBody, res => {
      if (res.status == 1) {
        return
      }
      that.setData({
        evaluates: res.data.list,
        score: res.data.score
      })
    })
  },
  switchTag: function(e) {
    //console.log(e)
    this.setData({
      tagIndex: e.currentTarget.id
    })
  },
  //添加购物车
  bindAddCart() {
    if (!userUtil.isLogin()) {
      userUtil.toLogin()
      return
    }
    var is = cartUtil.add(this.data.goods);
    if (is) util.successToast('添加成功')
    else util.failToast("该商品已添加过")
  },
  //跳转购物车页面
  noaToCart() {
    if (!userUtil.isLogin()) {
      userUtil.toLogin()
      return
    }
    wx.navigateTo({
      url: '../shop_cart/shop_cart',
    })
  },
  //首页
  navToHome() {
    wx.switchTab({
      url: '../nav_home/nav_home',
    })
  },
  //订单支付页面
  navToOrderPay() {
    if (!userUtil.isLogin()) {
      userUtil.toLogin()
      return
    }
    var goods = this.data.goods
    goods.num = 1
    app.data.selectGoods = [goods]
    var url = '../order_pay/order_pay'
    if (this.data.cusId)
      url += "?cusId=" + this.data.cusId
    wx.navigateTo({
      url: url
    })
  },
  //分享获利页面
  navToShare() {
    app.data.tempGoods = this.data.goods
    app.data.tempTui = this.data.tui
    wx.navigateTo({
      url: '../goods_share/goods_share'
    })
  }
})